<template>
	<view class="">
		<uni-popup ref='popup' type="center" border-radius="10px">
			<view class="gl-popup-view">
				<view class="gl-popup-title">
					<view class=""> {{title}} </view>
					<uni-icons class="gl-button-mini gl-button-error" type="closeempty" size="18" color="#fff"
						@tap="EventClose"></uni-icons>
				</view>
				<view class="gl-popup-body" style="display: flex;flex-direction: column;gap: 5px;">
					<uni-easyinput type="text" v-model="curData.name">
						<template #left>
							<view class="gl-item-title">名称:</view>
						</template>
					</uni-easyinput>
					<view class="" style="display: flex;gap: 10px;">
						<uni-easyinput type="digit" v-model="curData.money">
							<template #left>
								<view class="gl-item-title">个人业绩:</view>
							</template>
						</uni-easyinput>
						<uni-easyinput type="digit" v-model="curData.steam">
							<template #left>
								<view class="gl-item-title">团队业绩:</view>
							</template>
						</uni-easyinput>
					</view>
					<view class="" style="display: flex;gap: 10px;">
						<uni-easyinput type="digit" v-model="curData.discount">
							<template #left>
								<view class="gl-item-title">专享折扣:</view>
							</template>
						</uni-easyinput>
						<uni-easyinput type="digit" v-model="curData.draw_rate">
							<template #left>
								<view class="gl-item-title">董事分红:</view>
							</template>
						</uni-easyinput>
					</view>
					<view class="" style="display: flex;gap: 10px;">
						<uni-easyinput type="digit" v-model="curData.buyer">
							<template #left>
								<view class="gl-item-title">消费返点:</view>
							</template>
						</uni-easyinput>
						<uni-easyinput type="digit" v-model="curData.seller">
							<template #left>
								<view class="gl-item-title">销售业绩:</view>
							</template>
						</uni-easyinput>
					</view>
					<view class="" style="display: flex;gap: 10px;">
						<uni-data-select v-model="curData.level" :localdata="levelData" label="权益券类型"
							@change="levelChange">
						</uni-data-select>
						<uni-easyinput type="number" v-model="curData.count">
							<template #left>
								<view class="gl-item-title">权益券数量:</view>
							</template>
						</uni-easyinput>
						<uni-easyinput type="digit" v-model="curData.worth">
							<template #left>
								<view class="gl-item-title">抵扣红包:</view>
							</template>
						</uni-easyinput>
					</view>
				</view>
				<view class="gl-popup-foot" style="display: flex;justify-content: flex-end;gap: 10px;">
					<view class="gl-button gl-button-base" @tap="EventSubmit">
						<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">保存</text>
					</view>
					<view class="gl-button gl-button-warning" @tap="EventClose">
						<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">取消</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod'
	export default {
		data() {
			return {
				sConf: getApp().globalData,
				title: '修改会员权益',
				curData: {
					id: null,
					name: null,
					money: 0,
					steam: 0,
					discount: 0,
					draw_rate: 0,
					buyer: 0,
					seller: 0,
					count: 0,
					worth: 0,
					level: 2
				},
				curLevel: null,
				levelData: [{
					text: 'VIP会员',
					value: 2
				}, {
					text: '悦玺体验官',
					value: 3
				}, {
					text: '悦玺推荐管',
					value: 4
				}, {
					text: '悦玺运营官',
					value: 5
				}]
			}
		},
		methods: {
			EventClose() {
				this.$refs.popup.close()
			},
			open(e) {
				this.curData.id = e.id
				this.curData.name = e.name
				this.curData.money = (e.money / 100).toFixed(2)
				this.curData.steam = (e.steam / 100).toFixed(2)
				this.curData.discount = (e.discount / 100).toFixed(2)
				this.curData.draw_rate = (e.draw_rate / 100).toFixed(2)
				this.curData.buyer = (e.buyer / 1000).toFixed(3)
				this.curData.seller = (e.seller / 1000).toFixed(3)
				this.curData.worth = (e.worth / 100).toFixed(2)
				this.curData.count = e.sub_content?.count || 0
				this.curData.level = e.sub_content?.level || 0
				this.$refs.popup.open()
			},
			EventSubmit() {
				console.log('EventSubmit');
				let host = this.sConf.host + '/admin/level/' + this.curData.id + '/';
				let token = this.sConf.auth.token || '';
				let data = {
					name: this.curData.name,
					money: Math.ceil(this.curData.money * 100 || 0),
					steam: Math.ceil(this.curData.steam * 100 || 0),
					discount: Math.ceil(this.curData.discount * 100 || 0),
					draw_rate: Math.ceil(this.curData.draw_rate * 100 || 0),
					buyer: Math.ceil(this.curData.buyer * 1000 || 0),
					seller: Math.ceil(this.curData.seller * 1000 || 0),
					worth: Math.ceil(this.curData.worth * 100 || 0),
				}
				if (this.curData.level) {
					data.content = JSON.stringify({
						level: this.curData.level,
						count: this.curData.count
					})
				}
				console.log('args', data);
				psMethod.authenticatorPatchSync(host, token, data).then(res => {
					console.log('编辑权益', res);
					let that = this
					uni.showToast({
						title: '完成',
						success() {
							that.$emit('reload')
							that.EventClose()
						}
					})
				}).catch(err => {
					console.log('编辑权益__失败', err);
					uni.showToast({
						title: '失败',
						icon: 'error'
					})
				})
			},
			levelChange(e) {
				console.log('levelChange', e);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.gl-item-title {
		padding-left: 5px;
		color: #007aff;
		font-weight: bold;
	}
</style>